/**
 * create by ck on 2016/3/18.
 */
var iStudentId;
var iSchoolId;
var schoolList;
var studList;
var studName;
var iScheduleType = 2;
var iUserId;
var selectDate;
var defaultDate;
var postData;
var drawing = false;



$.fn.pagination.defaults.displayMsg = '';
$(document).ready(function(){
    selectDate = new Date().Format("yyyy-MM");
    defaultDate = new Date().Format("yyyy-MM-dd");
    var DataGrid2  = new DataGridEvent();
    schoolList = $('#ScheduleUser');
    studList   = $('#ScheduleList');
    $('.xh-datagrid-toolbar').parseToolbar(afterParseToolbar);
    schoolList.initDataGrid('getUserData');
    //学校列表加载后刷新学生列表
    DataGrid.onLoadSuccess = function(data){
        console.log(data);
        if(data['rows'].length <= 0) return false;
        iUserId = data['rows'][0].iUserId;
        studName = data['rows'][0].sStaffName;
        getSchedule();
    };
    //点击学校列表在学生列表显示相应学生
    DataGrid.onClickRow = function(rowIndex, rowData){
        studName = rowData.sStaffName;
        iUserId = rowData.iUserId;
        getSchedule()
    };
    $('#selfbuilt').click(function() {
        iScheduleType = 1;
        $(this).addClass('btn-primary').removeClass('btn-default');
        $('#distribution').addClass('btn-default').removeClass('btn-primary');
        getSchedule();
    });
    $('#distribution').click(function() {
        iScheduleType = 2;
        $(this).addClass('btn-primary').removeClass('btn-default');
        $('#selfbuilt').addClass('btn-default').removeClass('btn-primary');
        getSchedule();
    });
});

function getSchedule(){
    postData = {iUserId:iUserId,iScheduleType:iScheduleType,selectDate:selectDate};
    $.post(baseUrl + 'Schedule/getList', postData, function(result){
        $('#calendar').fullCalendar( 'destroy');
        fullCalendarData(result);
    }, 'json')

}

function fullCalendarData(listData) {
    $('#scheduleTitle').html(studName+'的日程管理');
    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'listDay,listWeek,month,listMonth'
        },
        // customize the button names,
        // otherwise they'd all just say "list"
        views: {
            listDay: { buttonText: '天' },
            listWeek: { buttonText: '周' }
        },
        defaultView: 'month',
        navLinks: true, // can click day/week names to navigate views
        editable: true,
        locale:'zh-cn',
        defaultDate: defaultDate,
        eventLimit: true, // allow "more" link when too many events
        dayClick: function(date, allDay, jsEvent, view) {
            var url = baseUrl + 'Schedule/addSchedule?action=add&iScheduleType='+iScheduleType+'&startTime='+(date._i/1000)+'&iUserId='+iUserId;
            window.top.xhTop.showModal({
                title: '添加日程',
                content: '<iframe frameborder="0" style="width: 100%;height:400px" src="' + url + '"></iframe>',

            });
        },
        eventClick: function(event, jsEvent, view) {
            console.log(event);
            var url = baseUrl + 'Schedule/addSchedule?action=edit&iScheduleId='+event.id;
            window.top.xhTop.showModal({
                title: '修改日程',
                content: '<iframe frameborder="0" style="width: 100%;height:400px" src="' + url + '"></iframe>',
            });
        },
        events:listData
    });
}
function afterParseToolbar() {
    $.overtFun.vagueSearch(schoolList, null, $('#SchoolList_Search'));
}

// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子：
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function formatDate(now) {
    var year=now.getYear();
    var month=now.getMonth()+1;
    var date=now.getDate();
    var hour=now.getHours();
    var minute=now.getMinutes();
    var second=now.getSeconds();
    return "20"+year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
}